﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Sustainalytics.Entities;
using Sustainalytics.Entities.ESGRatings;
using Sustainalytics.ESGRatings.BusinessLogic.ESGProfile;
using Sustainalytics.ESGRatings.Service.Controllers;
using Sustainalytics.Utils;
using Sustainalytics.Utils.WebApi.ErrorCodes;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Sustainalytics.ESGRatings.Service.Tests.Controllers
{
    [TestClass]
    public class ESGProfileControllerTests
    {
        private IESGProfileManagement ProfileManager { get; set; }

        private ESGProfileController ProfileController { get; set; }

        [TestInitialize]
        public void Initialize()
        {


            ProfileManager = ESGProfileManagement.CreateProfileManagement();
        }

        [TestMethod]
        public void Get_ESGProfile_by_AccountId_and_ProfileId()
        {
            //Create profile controller
            var httpReqMsg = new HttpRequestMessage();
            httpReqMsg.SetConfiguration(new HttpConfiguration());
            this.ProfileController = new ESGProfileController(this.ProfileManager)
            {
                Request = httpReqMsg
            };

            Guid accountId = new Guid("5d2aee53-87c2-e411-a90b-d89d676410ec");
            Guid profileId = new Guid("e0117d12-dbce-41e9-8920-0fa1485bca0f");

            ESGProfileDto freshProfile = new ESGProfileDto
            {
                Id = profileId,
                AccountId = accountId,
                AccountType = AccessLevel.Tier1,
                Active = ActiveTypeEnum.Active,
                Name = "ESGProfileROX_1",
                MatrixId = new Guid("290a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Default Weight Matrix",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets",

            };

            HttpResponseMessage putProfile = this.ProfileController.Post(freshProfile);
            HttpResponseMessage profile = ProfileController.Get(accountId, profileId);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");
        }

        [TestMethod]
        public void Put_ESGProfile()
        {
            //Create profile controller
            var httpReqMsg = new HttpRequestMessage();
            httpReqMsg.SetConfiguration(new HttpConfiguration());
            this.ProfileController = new ESGProfileController(this.ProfileManager)
            {
                Request = httpReqMsg
            };

            Guid accountId = new Guid("5d2aee53-87c2-e411-a90b-d89d676410ec");
            Guid profileId = new Guid("e0117d12-dbce-41e9-8920-0fa1485bca0f");

            ESGProfileDto freshProfile = new ESGProfileDto
            {
                Id = profileId,
                AccountId = accountId,
                AccountType = AccessLevel.Tier1,
                Active = ActiveTypeEnum.Active,
                Name = "ESGProfileROX_1",
                MatrixId = new Guid("290a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Default Weight Matrix",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets",

            };

            HttpResponseMessage putProfile = this.ProfileController.Post(freshProfile);
            HttpResponseMessage profile = ProfileController.Get(accountId, profileId);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");
        }
        

        [TestMethod]
        public void GetAll_ESGProfiles()
        {
            //Create profile controller
            var httpReqMsg = new HttpRequestMessage();
            httpReqMsg.SetConfiguration(new HttpConfiguration());
            this.ProfileController = new ESGProfileController(this.ProfileManager)
            {
                Request = httpReqMsg
            };

            Guid accountId = new Guid("4333d8a5-6745-e411-a648-2c59e5418dc4");
            Guid profileId1 = new Guid("3d33d8a5-6745-e411-a648-2c59e5418dc4");
            Guid profileId2 = new Guid("f0117d12-dbce-41e9-8920-0fa1485bca0f");
            Guid profileId3 = new Guid("5333d8a5-6745-e411-a648-2c59e5418dc4");

            //Delete them First, so addition will be done by the same Guid
            ProfileController.Delete(accountId, profileId1);
            ProfileController.Delete(accountId, profileId2);
            ProfileController.Delete(accountId, profileId3);

            ESGProfileDto freshProfile1 = new ESGProfileDto
            {
                Id = profileId1,
                AccountId = accountId,
                AccountType = AccessLevel.Tier1,
                Active = ActiveTypeEnum.Active,
                Name = "ESGProfileROX_E1",
                MatrixId = new Guid("390a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix X",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets",

            };

            HttpResponseMessage putProfile = this.ProfileController.Post(freshProfile1);

            HttpResponseMessage profile = ProfileController.Get(accountId, profileId1);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

            ESGProfileDto freshProfile2 = new ESGProfileDto
            {
                Id = profileId2,
                AccountId = accountId,
                AccountType = AccessLevel.Tier2,
                Active = ActiveTypeEnum.Suggested,
                Name = "ESGProfileROX_F1",
                MatrixId = new Guid("490a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix Y",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets",

            };

            putProfile = this.ProfileController.Post(freshProfile2);

            profile = ProfileController.Get(accountId, profileId2);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

            ESGProfileDto freshProfile3 = new ESGProfileDto
            {
                Id = profileId3,
                AccountId = accountId,
                AccountType = AccessLevel.Tier2,
                Active = ActiveTypeEnum.NotActive,
                Name = "ESGProfileROX_G1",
                MatrixId = new Guid("490a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix Z",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets Updated",

            };

            putProfile = this.ProfileController.Post(freshProfile3);

            profile = ProfileController.Get(accountId, profileId3);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

            var profilesForAccount = ProfileController.Get(accountId);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profilesForAccount, "Profiles list is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

            HttpContent profilesContent = profile.Content;
            Assert.IsNotNull(profilesContent, "HTTP Content response containing profiles list is null.");
        }

        [TestMethod]
        public void Get_Active_ESGProfile()
        {
            //Create profile controller
            var httpReqMsg = new HttpRequestMessage();
            httpReqMsg.SetConfiguration(new HttpConfiguration());
            this.ProfileController = new ESGProfileController(this.ProfileManager)
            {
                Request = httpReqMsg
            };

            Guid accountId = new Guid("72ad61fc-6012-e211-9a22-3c4a92dbc8a2");
            Guid profileId1 = Guid.NewGuid();
            Guid profileId2 = Guid.NewGuid();
            Guid profileId3 = Guid.NewGuid();

            //Delete them First, so addition will be done by the same Guid

            ProfileController.Delete(accountId, profileId1);
            ProfileController.Delete(accountId, profileId2);
            ProfileController.Delete(accountId, profileId3);

            ESGProfileDto freshProfile1 = new ESGProfileDto
            {
                Id = profileId1,
                AccountId = accountId,
                AccountType = AccessLevel.Tier1,
                Active = ActiveTypeEnum.Active,
                Name = "ESGProfileROX_E1",
                MatrixId = new Guid("390a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix X",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets",

            };

            HttpResponseMessage putProfile = this.ProfileController.Post(freshProfile1);

            HttpResponseMessage profile = ProfileController.Get(accountId, profileId1);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

            ESGProfileDto freshProfile2 = new ESGProfileDto
            {
                Id = profileId2,
                AccountId = accountId,
                AccountType = AccessLevel.Tier2,
                Active = ActiveTypeEnum.Suggested,
                Name = "ESGProfileROX_F1",
                MatrixId = new Guid("490a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix Y",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets",

            };

            putProfile = this.ProfileController.Post(freshProfile2);

            profile = ProfileController.Get(accountId, profileId2);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

            ESGProfileDto freshProfile3 = new ESGProfileDto
            {
                Id = profileId3,
                AccountId = accountId,
                AccountType = AccessLevel.Tier2,
                Active = ActiveTypeEnum.NotActive,
                Name = "ESGProfileROX_G1",
                MatrixId = new Guid("490a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix Z",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets Updated",

            };

            putProfile = this.ProfileController.Post(freshProfile3);

            profile = ProfileController.Get(accountId, profileId3);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

            var profilesForAccount = ProfileController.Get(accountId);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profilesForAccount, "Profiles list is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

            HttpContent profilesContent = profile.Content;
            Assert.IsNotNull(profilesContent, "HTTP Content response containing profiles list is null.");

            profile = ProfileController.Get(accountId, ActiveTypeEnum.Active);
            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");
        }

        [TestMethod]
        public void Get_Default_ESGProfile()
        {
            //Create profile controller
            var httpReqMsg = new HttpRequestMessage();
            httpReqMsg.SetConfiguration(new HttpConfiguration());
            this.ProfileController = new ESGProfileController(this.ProfileManager)
            {
                Request = httpReqMsg
            };

            HttpResponseMessage getDefaultProfile = this.ProfileController.Get(null);

            Assert.IsNotNull(getDefaultProfile, "HTTP response is null.");
            Assert.IsTrue(getDefaultProfile.StatusCode == HttpStatusCode.OK, "Default ESG Profile response status code is not ok!");
        }
        
        [TestMethod]
        public void Put_ESGProfile_Multiple_ActiveProfiles_Transition_From_NotActive_To_Suggested()
        {

            //Create profile controller
            var httpReqMsg = new HttpRequestMessage();
            httpReqMsg.SetConfiguration(new HttpConfiguration());
            this.ProfileController = new ESGProfileController(this.ProfileManager)
            {
                Request = httpReqMsg
            };

            Guid accountId = new Guid("a6ad61fc-6012-e211-9a22-3c4a92dbc8a2");

            Guid profileId1 = Guid.NewGuid();
            Guid profileId2 = Guid.NewGuid();
            Guid profileId3 = Guid.NewGuid();

            ProfileController.Delete(accountId, profileId1);
            ProfileController.Delete(accountId, profileId2);
            ProfileController.Delete(accountId, profileId3);

            ESGProfileDto freshProfile1 = new ESGProfileDto
            {
                Id = profileId1,
                AccountId = accountId,
                AccountType = AccessLevel.Tier1,
                Active = ActiveTypeEnum.NotActive,
                Name = "ESGProfileROX_C1",
                MatrixId = new Guid("390a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix 1",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets 1",

            };

            HttpResponseMessage putProfile = this.ProfileController.Post(freshProfile1);

            HttpResponseMessage profile = ProfileController.Get(accountId, profileId1);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

            ESGProfileDto freshProfile2 = new ESGProfileDto
            {
                Id = profileId2,
                AccountId = accountId,
                AccountType = AccessLevel.Tier2,
                Active = ActiveTypeEnum.Active,
                Name = "ESGProfileROX_C2",
                MatrixId = new Guid("490a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix 1",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets 2",

            };

            putProfile = this.ProfileController.Post(freshProfile2);
            profile = ProfileController.Get(accountId, profileId2);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");


            ESGProfileDto freshProfile3 = new ESGProfileDto
            {
                Id = profileId3,
                AccountId = accountId,
                AccountType = AccessLevel.Tier2,
                Active = ActiveTypeEnum.NotActive,
                Name = "ESGProfileROX_C3",
                MatrixId = new Guid("490a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix Z",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets Updated 3",

            };

            putProfile = this.ProfileController.Post(freshProfile3);
            profile = ProfileController.Get(accountId, profileId3);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");

            ESGProfileDto update_profile3 = new ESGProfileDto
            {
                Id = profileId3,
                AccountId = accountId,
                AccountType = AccessLevel.Tier2,
                Active = ActiveTypeEnum.Active,
                Name = "ESGProfileROX_C3_Updated",
                MatrixId = new Guid("490a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix 1 Updated",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets Updated ccc",

            };

            putProfile = this.ProfileController.Put(update_profile3);

            profile = ProfileController.Get(accountId, profileId2);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");
        }

        [TestMethod]
        public void Put_ESGProfile_Multiple_ActiveProfiles_Transition_From_Suggested_To_Active()
        {
            //Create profile controller
            var httpReqMsg = new HttpRequestMessage();
            httpReqMsg.SetConfiguration(new HttpConfiguration());
            this.ProfileController = new ESGProfileController(this.ProfileManager)
            {
                Request = httpReqMsg
            };

            Guid accountId = new Guid("4933d8a5-6745-e411-a648-2c59e5418dc4");

            Guid profileId1 = Guid.NewGuid();
            Guid profileId2 = Guid.NewGuid();
            Guid profileId3 = Guid.NewGuid();

            ProfileController.Delete(accountId, profileId1);
            ProfileController.Delete(accountId, profileId2);
            ProfileController.Delete(accountId, profileId3);

            ESGProfileDto freshProfile1 = new ESGProfileDto
            {
                Id = profileId1,
                AccountId = accountId,
                AccountType = AccessLevel.Tier1,
                Active = ActiveTypeEnum.NotActive,
                Name = "ESGProfileROX_B1",
                MatrixId = new Guid("390a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix 1",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets 1",

            };

            HttpResponseMessage putProfile = this.ProfileController.Post(freshProfile1);

            HttpResponseMessage profile = ProfileController.Get(accountId, profileId1);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

            ESGProfileDto freshProfile2 = new ESGProfileDto
            {
                Id = profileId2,
                AccountId = accountId,
                AccountType = AccessLevel.Tier2,
                Active = ActiveTypeEnum.NotActive,
                Name = "ESGProfileROX_B2",
                MatrixId = new Guid("490a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix 1",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets 2",

            };

            putProfile = this.ProfileController.Post(freshProfile2);
            profile = ProfileController.Get(accountId, profileId2);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");


            ESGProfileDto freshProfile3 = new ESGProfileDto
            {
                Id = profileId3,
                AccountId = accountId,
                AccountType = AccessLevel.Tier2,
                Active = ActiveTypeEnum.Suggested,
                Name = "ESGProfileROX_A3",
                MatrixId = new Guid("490a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix Z",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets Updated 3",

            };

            putProfile = this.ProfileController.Post(freshProfile3);
            profile = ProfileController.Get(accountId, profileId3);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");

            ESGProfileDto update_profile3 = new ESGProfileDto
            {
                Id = profileId3,
                AccountId = accountId,
                AccountType = AccessLevel.Tier2,
                Active = ActiveTypeEnum.Active,
                Name = "ESGProfileROX_A3_Updated",
                MatrixId = new Guid("490a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix 1 Updated",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets Updated aaa",

            };

            putProfile = this.ProfileController.Put(update_profile3);

            profile = ProfileController.Get(accountId, profileId2);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");
        }
        [TestMethod]
        public void Put_ESGProfile_Multiple_ActiveProfiles_Transition_From_Inactive_To_Suggested()
        {
            //Create profile controller
            var httpReqMsg = new HttpRequestMessage();
            httpReqMsg.SetConfiguration(new HttpConfiguration());
            this.ProfileController = new ESGProfileController(this.ProfileManager)
            {
                Request = httpReqMsg
            };

            Guid accountId = new Guid("70ad61fc-6012-e211-9a22-3c4a92dbc8a2");

            Guid profileId1 = Guid.NewGuid();
            Guid profileId2 = Guid.NewGuid();
            Guid profileId3 = Guid.NewGuid();

            ProfileController.Delete(accountId, profileId1);
            ProfileController.Delete(accountId, profileId2);
            ProfileController.Delete(accountId, profileId3);

            ESGProfileDto freshProfile1 = new ESGProfileDto
            {
                Id = profileId1,
                AccountId = accountId,
                AccountType = AccessLevel.Tier1,
                Active = ActiveTypeEnum.Active,
                Name = "ESGProfileROX_A1",
                MatrixId = new Guid("390a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix 1",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets 1",

            };

            HttpResponseMessage putProfile = this.ProfileController.Post(freshProfile1);

            HttpResponseMessage profile = ProfileController.Get(accountId, profileId1);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

            ESGProfileDto freshProfile2 = new ESGProfileDto
            {
                Id = profileId2,
                AccountId = accountId,
                AccountType = AccessLevel.Tier2,
                Active = ActiveTypeEnum.NotActive,
                Name = "ESGProfileROX_A2",
                MatrixId = new Guid("490a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix 1",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets 2",

            };

            putProfile = this.ProfileController.Post(freshProfile2);
            profile = ProfileController.Get(accountId, profileId2);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");


            ESGProfileDto freshProfile3 = new ESGProfileDto
            {
                Id = profileId3,
                AccountId = accountId,
                AccountType = AccessLevel.Tier2,
                Active = ActiveTypeEnum.NotActive,
                Name = "ESGProfileROX_A3",
                MatrixId = new Guid("490a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix Z",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets Updated 3",

            };

            putProfile = this.ProfileController.Post(freshProfile3);
            profile = ProfileController.Get(accountId, profileId3);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");

            ESGProfileDto update_profile3 = new ESGProfileDto
            {
                Id = profileId3,
                AccountId = accountId,
                AccountType = AccessLevel.Tier2,
                Active = ActiveTypeEnum.Active,
                Name = "ESGProfileROX_A3_Updated",
                MatrixId = new Guid("490a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix 1 Updated",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets Updated aaa",

            };

            putProfile = this.ProfileController.Put(update_profile3);

            profile = ProfileController.Get(accountId, profileId2);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");
        }


        [TestMethod]
        public void Put_ESGProfile_Attempt_MultipleProfilesActive()
        {
            //Create profile controller
            var httpReqMsg = new HttpRequestMessage();
            httpReqMsg.SetConfiguration(new HttpConfiguration());
            this.ProfileController = new ESGProfileController(this.ProfileManager)
            {
                Request = httpReqMsg
            };

            Guid accountId = new Guid("84ad61fc-6012-e211-9a22-3c4a92dbc8a2");

            Guid profileId1 = Guid.NewGuid();
            Guid profileId2 = Guid.NewGuid();
            Guid profileId3 = Guid.NewGuid();

            ProfileController.Delete(accountId, profileId1);
            ProfileController.Delete(accountId, profileId2);
            ProfileController.Delete(accountId, profileId3);

            ESGProfileDto freshProfile1 = new ESGProfileDto
            {
                Id = profileId1,
                AccountId = accountId,
                AccountType = AccessLevel.Tier1,
                Active = ActiveTypeEnum.Active,
                Name = "ESGProfileROX_2",
                MatrixId = new Guid("390a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix 1",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets 1",

            };

            HttpResponseMessage putProfile = this.ProfileController.Post(freshProfile1);

            HttpResponseMessage profile = ProfileController.Get(accountId, profileId1);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

            ESGProfileDto freshProfile2 = new ESGProfileDto
            {
                Id = profileId2,
                AccountId = accountId,
                AccountType = AccessLevel.Tier2,
                Active = ActiveTypeEnum.Suggested,
                Name = "ESGProfileROX_3",
                MatrixId = new Guid("490a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix 1",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets 2",

            };

            putProfile = this.ProfileController.Post(freshProfile2);
            profile = ProfileController.Get(accountId, profileId2);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");


            ESGProfileDto freshProfile3 = new ESGProfileDto
            {
                Id = profileId3,
                AccountId = accountId,
                AccountType = AccessLevel.Tier2,
                Active = ActiveTypeEnum.NotActive,
                Name = "ESGProfileROX_G1",
                MatrixId = new Guid("490a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix Z",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets Updated 3",

            };

            putProfile = this.ProfileController.Post(freshProfile3);
            profile = ProfileController.Get(accountId, profileId3);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");

            ESGProfileDto update_profile3 = new ESGProfileDto
            {
                Id = profileId3,
                AccountId = accountId,
                AccountType = AccessLevel.Tier2,
                Active = ActiveTypeEnum.Active,
                Name = "ESGProfileROX_3_Updated",
                MatrixId = new Guid("490a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix 1 Updated",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets Updated xx",

            };

            putProfile = this.ProfileController.Put(update_profile3);

            profile = ProfileController.Get(accountId, profileId2);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

        }

        [TestMethod]
        public void Put_Default_ESGProfile__Invalid_Operation()
        {
            //Create profile controller
            var httpReqMsg = new HttpRequestMessage();
            httpReqMsg.SetConfiguration(new HttpConfiguration());
            this.ProfileController = new ESGProfileController(this.ProfileManager)
            {
                Request = httpReqMsg
            };

            Guid profileIdDefault = new Guid("15c3b0e2-c87d-4d2a-ad3f-e16b586a2ffe");

            ESGProfileDto freshProfileDefault = new ESGProfileDto
            {
                Id = profileIdDefault,
                AccountId = null,
                AccountType = AccessLevel.Tier1,
                Active = ActiveTypeEnum.Active,
                Name = "DEFAULT EDITED",
                MatrixId = new Guid("290a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Default Weight Matrix",
                ReferenceUniverseId = new Guid("71f2972e-3153-4f83-ac6e-d82bd676481f"),
                ReferenceUniverseName = "Default",

            };

            HttpResponseMessage patchProfile = this.ProfileController.Put(freshProfileDefault);

            Assert.IsNotNull(patchProfile, "HTTP response is null.");
            Assert.IsTrue(patchProfile.StatusCode == HttpStatusCode.BadRequest, "HTTP Status Code is not the expected one.");

            var objectReturned = ((ObjectContent)(patchProfile.Content)).Value;
            Assert.IsTrue(JsonConvert.SerializeObject(objectReturned) == JsonConvert.SerializeObject(new { ErrorStatusCode = WebAPIGeneralErrorCodes.OperationNotAllowed }), "The error is not the expected one.");

        }

        [TestMethod]
        public void Put_ESGProfile_by_WholeObjectDto()
        {
            //Create profile controller
            var httpReqMsg = new HttpRequestMessage();
            httpReqMsg.SetConfiguration(new HttpConfiguration());
            this.ProfileController = new ESGProfileController(this.ProfileManager)
            {
                Request = httpReqMsg
            };

            Guid accountId = new Guid("5d2aee53-87c2-e411-a90b-d89d676410ec");
            Guid profileId1 = new Guid("a0117d12-dbce-41e9-8920-0fa1485bca0f");
            Guid profileId2 = new Guid("b0117d12-dbce-41e9-8920-0fa1485bca0f");

            ProfileController.Delete(accountId, profileId1);
            ProfileController.Delete(accountId, profileId2);

            ESGProfileDto freshProfile1 = new ESGProfileDto
            {
                Id = profileId1,
                AccountId = accountId,
                AccountType = AccessLevel.Tier1,
                Active = ActiveTypeEnum.Active,
                Name = "ESGProfileROX_2",
                MatrixId = new Guid("390a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix 1",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets",

            };

            HttpResponseMessage putProfile = this.ProfileController.Post(freshProfile1);

            HttpResponseMessage profile = ProfileController.Get(accountId, profileId1);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

            ESGProfileDto freshProfile2 = new ESGProfileDto
            {
                Id = profileId2,
                AccountId = accountId,
                AccountType = AccessLevel.Tier2,
                Active = ActiveTypeEnum.Suggested,
                Name = "ESGProfileROX_3",
                MatrixId = new Guid("490a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix 1",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets",

            };

            putProfile = this.ProfileController.Post(freshProfile2);

            profile = ProfileController.Get(accountId, profileId2);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

            ESGProfileDto update_freshProfile = new ESGProfileDto
            {
                Id = profileId2,
                AccountId = accountId,
                AccountType = AccessLevel.Tier2,
                Active = ActiveTypeEnum.NotActive,
                Name = "ESGProfileROX_3_Updated",
                MatrixId = new Guid("490a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix 1 Updated",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets Updated",

            };

            putProfile = this.ProfileController.Put(update_freshProfile);

            profile = ProfileController.Get(accountId, profileId2);

            Assert.IsNotNull(putProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

        }

        [TestMethod]
        public void Delete_ESGProfile_In_Suggested_ActiveType__Valid_Operation()
        {
            //Create profile controller
            var httpReqMsg = new HttpRequestMessage();
            httpReqMsg.SetConfiguration(new HttpConfiguration());
            this.ProfileController = new ESGProfileController(this.ProfileManager)
            {
                Request = httpReqMsg
            };

            Guid accountId = Guid.NewGuid();
            Guid profileId3 = Guid.NewGuid();

            HttpResponseMessage delMsgProfile = this.ProfileController.Delete(accountId, profileId3);

            ESGProfileDto freshProfile1 = new ESGProfileDto
            {
                Id = profileId3,
                AccountId = accountId,
                AccountType = AccessLevel.Tier1,
                Active = ActiveTypeEnum.Suggested,
                Name = "ESGProfileROX_4",
                MatrixId = new Guid("390a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix 4",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets 4",

            };

            HttpResponseMessage retMsgProfile = this.ProfileController.Post(freshProfile1);

            HttpResponseMessage profile = ProfileController.Get(accountId, profileId3);

            Assert.IsNotNull(retMsgProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

            retMsgProfile = this.ProfileController.Delete(accountId, profileId3);

            profile = ProfileController.Get(accountId, profileId3);

            Assert.IsNotNull(retMsgProfile, "HTTP response is null.");
            Assert.IsNull(profile.Content, "Profile is not null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.NotFound, "Profile response status code is not ok!");
        }

        [TestMethod]
        public void Delete_ESGProfile_In_Active_ActiveType__Invalid_Operation()
        {
            //Create profile controller
            var httpReqMsg = new HttpRequestMessage();
            httpReqMsg.SetConfiguration(new HttpConfiguration());
            this.ProfileController = new ESGProfileController(this.ProfileManager)
            {
                Request = httpReqMsg
            };

            Guid accountId = Guid.NewGuid();
            Guid profileId3 = Guid.NewGuid();

            HttpResponseMessage delMsgProfile = this.ProfileController.Delete(accountId, profileId3);

            ESGProfileDto freshProfile1 = new ESGProfileDto
            {
                Id = profileId3,
                AccountId = accountId,
                AccountType = AccessLevel.Tier1,
                Active = ActiveTypeEnum.Active,
                Name = "ESGProfileROX_New_Active",
                MatrixId = new Guid("390a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix New",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets 4",

            };

            HttpResponseMessage retMsgProfile = this.ProfileController.Post(freshProfile1);

            HttpResponseMessage profile = ProfileController.Get(accountId, profileId3);

            Assert.IsNotNull(retMsgProfile, "HTTP response is null.");
            Assert.IsNotNull(profile, "Profile is null.");
            Assert.IsTrue(profile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

            retMsgProfile = this.ProfileController.Delete(accountId, profileId3);

            var objectReturned = ((ObjectContent)(retMsgProfile.Content)).Value;

            Assert.IsTrue(retMsgProfile.StatusCode == HttpStatusCode.BadRequest, "HTTP Status Code is not the expected one.");
            Assert.IsTrue(JsonConvert.SerializeObject(objectReturned) == JsonConvert.SerializeObject(new { ErrorStatusCode = 1090 }), "The error is not the expected one.");
        }

        [TestMethod]
        public void Delete_ESGProfile_Default_Invalid_Operation()
        {
            //Create profile controller
            var httpReqMsg = new HttpRequestMessage();
            httpReqMsg.SetConfiguration(new HttpConfiguration());
            this.ProfileController = new ESGProfileController(this.ProfileManager)
            {
                Request = httpReqMsg
            };

            Guid profileId3 = new Guid("15c3b0e2-c87d-4d2a-ad3f-e16b586a2ffe");

            HttpResponseMessage retMsgProfile = this.ProfileController.Delete(null, profileId3);

            var objectReturned = ((ObjectContent)(retMsgProfile.Content)).Value;

            Assert.IsTrue(retMsgProfile.StatusCode == HttpStatusCode.BadRequest, "HTTP Status Code is not the expected one.");

            Assert.IsTrue(JsonConvert.SerializeObject(objectReturned) == JsonConvert.SerializeObject(new { ErrorStatusCode = 1090 }), "The error is not the expected one.");
        }

        [TestMethod]
        public void Post_ESGProfile_Nr21__Invalid_Operation()
        {
            //Create profile controller
            var httpReqMsg = new HttpRequestMessage();
            httpReqMsg.SetConfiguration(new HttpConfiguration());
            this.ProfileController = new ESGProfileController(this.ProfileManager)
            {
                Request = httpReqMsg
            };

            Guid accountId = new Guid("5d2aee53-87c2-e411-a90b-d89d676410ec"); //This GUID has 20 accounts already...

            HttpResponseMessage profilesCount = ProfileController.Get(accountId);
            Assert.IsNotNull(profilesCount, "HTTP response is null.");
            Assert.IsNotNull(profilesCount.Content, "Profile is null.");
            Assert.IsTrue(profilesCount.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

            var objReturned = ((ObjectContent)(profilesCount.Content)).Value;
            List<ESGProfileDto> objList = (List<ESGProfileDto>)objReturned;
            Assert.IsTrue(objList.Count == 20, "Profile response status code is not ok!");

            Guid profileIdNew3 = Guid.NewGuid();

            ESGProfileDto freshProfile1 = new ESGProfileDto
            {
                Id = profileIdNew3,
                AccountId = accountId,
                AccountType = AccessLevel.Tier1,
                Active = ActiveTypeEnum.NotActive,
                Name = "ESGProfileROX_New_NotActive",
                MatrixId = new Guid("390a624d-a1bf-4298-b91d-2456223923d5"),
                MatrixName = "Weight Matrix New",
                ReferenceUniverseId = new Guid("da39fa47-5b26-4eaf-b91c-6badd75fb491"),
                ReferenceUniverseName = "MSCI Developed and Emerging Markets 4",

            };

            HttpResponseMessage retMsgProfile = this.ProfileController.Post(freshProfile1);
            var objectReturned = ((ObjectContent)(retMsgProfile.Content)).Value;

            Assert.IsTrue(retMsgProfile.StatusCode == HttpStatusCode.BadRequest, "HTTP Status Code is not the expected one.");
            Assert.IsTrue(JsonConvert.SerializeObject(objectReturned) == JsonConvert.SerializeObject(new { ErrorStatusCode = WebAPIGeneralErrorCodes.OperationNotAllowed }), "The error is not the expected one.");
        }

        [TestMethod]
        public void Get_ESGProfiles_With_List_Options()
        {
            //Create profile controller
            var httpReqMsg = new HttpRequestMessage();
            httpReqMsg.SetConfiguration(new HttpConfiguration());
            this.ProfileController = new ESGProfileController(this.ProfileManager)
            {
                Request = httpReqMsg
            };

            Guid accountId = new Guid("5d2aee53-87c2-e411-a90b-d89d676410ec"); //This GUID has 20 accounts already...

            Guid profileId3 = Guid.NewGuid();

            ListOptions defListOpt = new ListOptions();
            defListOpt.Skip = 0;
            defListOpt.Take = 3;
            HttpResponseMessage retMsgProfile = this.ProfileController.Get(accountId, defListOpt);

            Assert.IsNotNull(retMsgProfile, "HTTP response is null.");

            Assert.IsTrue(retMsgProfile.StatusCode == HttpStatusCode.OK, "Profile response status code is not ok!");

            var objectReturned = ((ObjectContent)(retMsgProfile.Content)).Value;
            List<ESGProfileDto> objList = (List<ESGProfileDto>)objectReturned;
            Assert.IsTrue(objList.Count == 3, "Profile response status code is not ok!");
        }
    }
}
